package com.cc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.entity.PageResult;
import com.cc.entity.Products;
import com.cc.service.IProductsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
@Tag(name = "商品管理", description = "商品相关接口")
public class ProductController {

    @Autowired
    private IProductsService productService;

    @GetMapping("/page")
    @Operation(summary = "获取商品分页数据", description = "分页查询商品信息")
    public PageResult<Products> getProductPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") int pageSize) {
        Page<Products> page = new Page<>(pageNum, pageSize);
        return productService.getPage(page);
    }

    @PostMapping
    @Operation(summary = "添加商品")
    public boolean addProduct(@RequestBody Products products) {
        return productService.save(products);
    }

    @PutMapping
    @Operation(summary = "修改商品")
    public boolean updateProduct(@RequestBody Products products) {
        return productService.updateById(products);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品")
    public boolean deleteProduct(@Parameter(description = "商品ID") @PathVariable Long id) {
        return productService.removeById(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取商品详情")
    public Products getProduct(@Parameter(description = "商品ID") @PathVariable Long id) {
        return productService.getById(id);
    }
}